/**
 * Copyright 2010 Google Inc.
 *
 * Main javascript for a latitude mashup.
 *
 */

goog.provide('fsl.buzz');
goog.provide('fsl.latitude');
goog.provide('fsl.foursquare');

goog.require('fsl.ui');

goog.require('goog.dom');
goog.require('goog.events');
goog.require('goog.fx.DragDrop');
goog.require('goog.fx.DragDropItem');
goog.require('goog.fx.DragDropGroup');
goog.require('goog.json');
goog.require('goog.net.XhrIo');
goog.require('goog.ui.Button');
goog.require('goog.ui.FormPost');
goog.require('goog.ui.Zippy');
goog.require('goog.Uri');
goog.require('goog.Uri.QueryData');

goog.require('soy');

goog.global.CLOSURE_NO_DEPS = false;

/**
 * Retrieve JSON data from Latitude.
 */
fsl.latitude.current = function() {
  goog.net.XhrIo.send('latitude?OP=currentLocation', function(e) {
    var xhr = e.target;
    var currentResponseJson = xhr.getResponseJson();
    var loc = currentResponseJson.data.latitude + ',' + currentResponseJson.data.longitude;
    var time = currentResponseJson.data.timestampMs;
    //goog.dom.getElement('latest').innerHTML =  fsl.ui.latitudeLocation({location: loc});
    var newDiv = goog.dom.createDom('div', {}, fsl.ui.latitudeLocation(
        {location: loc, time: time}));
    goog.dom.appendChild(goog.dom.getElement('latest'), newDiv);
  });
};

/**
 * Retrieve JSON data from Foursquare.
 */
fsl.foursquare.user = function() {
  var queryData = new goog.Uri.QueryData();
  queryData.add('OP', 'user');
  var path = 'foursquare?' + queryData.toString();
  goog.net.XhrIo.send(path, function(e) {
    var xhr = e.target;
    var currentResponseJson = xhr.getResponseJson();
    var loc = currentResponseJson.user.checkin.venue.name;
    var time = currentResponseJson.user.checkin.created;
    var newDiv = goog.dom.createDom('div', {}, fsl.ui.foursquareLocation(
        {location: loc, time: time}));
    goog.dom.appendChild(goog.dom.getElement('latest'), newDiv);
  });
};

/**
 * Retrieve JSON data from Foursquare.
 */
fsl.foursquare.venues = function(latitude, longitude) {
  // TODO(jlapenna): finish implementing.
  var queryData = goog.Uri.QueryData();
  queryData.add('OP', 'venues');
  queryData.add('geolat', latitude);
  queryData.add('geolong', longitude);
  var path = 'foursquare?' + queryData.toString();
  goog.net.XhrIo.send(path, function(e) {
    var xhr = e.target;
    var currentResponseJson = xhr.getResponseJson();
    alert(currentResponseJson);
    var loc = currentResponseJson.data.latitude + ',' + currentResponseJson.data.longitude;
    //goog.dom.getElement('latest').innerHTML =  fsl.ui.latitudeLocation({location: loc});
    var newSpan = goog.dom.createDom('span', {}, fsl.ui.latitudeLocation(
        {location: loc, time: time}));
    goog.dom.appendChild(goog.dom.getElement('latest'), newSpan);
  });
};

/**
 * Retrieve JSON data from Buzz.
 */
fsl.buzz.search = function(latitude, longitude) {
  // TODO(jlapenna): finish implementing.
  var queryData = new goog.Uri.QueryData();
  queryData.add('OP', 'search');
  queryData.add('latitude', latitude);
  queryData.add('longitude', longitude);
  var path = 'buzz?' + queryData.toString();
  goog.net.XhrIo.send(path, function(e) {
    var xhr = e.target;
    var currentResponseJson = xhr.getResponseJson();
    var loc = currentResponseJson.user.checkin.venue.name;
    var time = currentResponseJson.user.checkin.created;
    var newDiv = goog.dom.createDom('div', {}, fsl.ui.buzzLocation(
        {location: loc, time: time}));
    goog.dom.appendChild(goog.dom.getElement('latest'), newDiv);
  });
};

/**
 * Entry point for /nearbyjs.
 */
fsl.nearbyJs = function() {
  var newHeader = goog.dom.createDom('h1', {'style': 'background-color:#EEE'},
    'Hello world!');
  goog.dom.appendChild(document.body, newHeader);
  fsl.latitude.current();
  fsl.foursquare.user();
};
goog.exportSymbol('fsl.nearbyJs', fsl.nearbyJs);
